/**
 * @desc 线上图片转base64
 */
function getBase64Image(img) {
  const canvas = document.createElement("canvas");
  canvas.width = img.width;
  canvas.height = img.height;
  const ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0, 0, img.width, img.height);
  const dataURL = canvas.toDataURL("image/png");  // 可选其他值 image/jpeg
  return dataURL;
}

function main(src, cb) {
  const image = new Image();
  image.src = src; // 处理缓存
  image.crossOrigin = "*";  // 支持跨域图片
  image.onload = () => {
    const base64 = getBase64Image(image);
    cb && cb(base64);
    return base64
  }
  image.onerror = (error) => {
    const base64 = ""
    console.log("error=base64=00=", error)
    cb && cb(base64);
    return base64
  }
}


export { main }